<template>
  <pageContainer class="menu-container">
    <template slot="header">
      <div class="title">
        <i class="line"></i>
        <p class="text">查询条件</p>
      </div>
      <div class="search">
        <el-form :model="queryParams" ref="queryForm" :inline="true" class="demo-form-inline">
          <el-form-item label="菜单名称" prop="menuName">
            <el-input
              v-model="queryParams.menuName"
              placeholder="请输入菜单名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="菜单状态"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in statusOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh"
              size="mini"
              @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
            <el-button
              type="info"
              plain
              icon="el-icon-sort"
              size="mini"
              @click="toggleExpandAll"
            >展开/折叠</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <template slot="center">
      <div class="title">
        <i class="line"></i>
        <p class="text">列表</p>
      </div>
      <div class="table" ref="table">
        <el-table
          style="width:100%;height:100%"
          v-if="refreshTable"
          v-loading="loading"
          element-loading-background="#0c233e"
          element-loading-text="拼命加载中"
          :data="menuList"
          row-key="menuId"
          :header-cell-style="{textAlign: 'center',color: '#fff',backgroundColor: '#223450'}"
          :default-expand-all="isExpandAll"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <el-table-column prop="menuName" label="菜单名称" :show-overflow-tooltip="true" width="160" align="center"></el-table-column>
          <el-table-column prop="orderNum" label="排序" width="60" align="center"></el-table-column>
          <el-table-column prop="perms" label="权限标识" :show-overflow-tooltip="true" align="center"></el-table-column>
          <el-table-column prop="component" label="组件路径" :show-overflow-tooltip="true" align="center"></el-table-column>
          <el-table-column prop="status" label="状态" width="80" align="center">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:menu:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="handleAdd(scope.row)"
                v-hasPermi="['system:menu:add']"
              >新增</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:menu:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </template>
    <template slot="bottom">
      <!-- 添加或修改菜单对话框 -->
      <el-dialog v-if="open" :title="title" :visible.sync="open" width="680px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="上级菜单">
                <treeselect
                  v-model="form.parentId"
                  :options="menuOptions"
                  :normalizer="normalizer"
                  :show-count="true"
                  placeholder="选择上级菜单"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="菜单类型" prop="menuType">
                <el-radio-group v-model="form.menuType">
                  <el-radio label="M">目录</el-radio>
                  <el-radio label="C">菜单</el-radio>
                  <el-radio label="F">按钮</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="菜单名称" prop="menuName">
                <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="显示排序" prop="orderNum">
                <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
              </el-form-item>
            </el-col>
            <el-col :span="title === '修改菜单' ? 24 : 12 " v-if="form.menuType != 'F'">
              <el-form-item prop="path">
                <span slot="label">
                  <el-tooltip content="访问的路由地址，如：`user`，如外网地址需内链访问则以`http(s)://`开头" placement="top">
                  <i class="el-icon-question"></i>
                  </el-tooltip>
                  路由地址
                </span>
                <el-input v-model="form.path" placeholder="请输入路由地址" />
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType == 'C'">
              <el-form-item prop="component">
                <span slot="label">
                  <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
                  <i class="el-icon-question"></i>
                  </el-tooltip>
                  组件路径
                </span>
                <el-input v-model="form.component" placeholder="请输入组件路径" />
              </el-form-item>
            </el-col>
            <el-col :span="title === '修改菜单' ? 24 : 12 " v-if="form.menuType != 'M'">
              <el-form-item>
                <el-input v-model="form.perms" placeholder="请输入权限标识" maxlength="100" />
                <span slot="label">
                  <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasPermi('system:user:list')`)" placement="top">
                  <i class="el-icon-question"></i>
                  </el-tooltip>
                  权限字符
                </span>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType == 'C'">
              <el-form-item>
                <el-input v-model="form.query" placeholder="请输入路由参数" maxlength="255" />
                <span slot="label">
                  <el-tooltip content='访问路由的默认传递参数，如：`{"id": 1, "name": "ry"}`' placement="top">
                  <i class="el-icon-question"></i>
                  </el-tooltip>
                  路由参数
                </span>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType == 'C'">
              <el-form-item>
                <span slot="label">
                  <el-tooltip content="选择是则会被`keep-alive`缓存，需要匹配组件的`name`和地址保持一致" placement="top">
                  <i class="el-icon-question"></i>
                  </el-tooltip>
                  是否缓存
                </span>
                <el-radio-group v-model="form.isCache">
                  <el-radio label="0">缓存</el-radio>
                  <el-radio label="1">不缓存</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="title === '修改菜单' ? 24 : 12 " v-if="form.menuType != 'F'">
              <el-form-item>
                <span slot="label">
                  <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
                  <i class="el-icon-question"></i>
                  </el-tooltip>
                  显示状态
                </span>
                <el-radio-group v-model="form.visible">
                  <el-radio
                    v-for="dict in dict.type.sys_show_hide"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.menuType != 'F'">
              <el-form-item>
                <span slot="label">
                  <el-tooltip content="选择停用则路由将不会出现在侧边栏，也不能被访问" placement="top">
                  <i class="el-icon-question"></i>
                  </el-tooltip>
                  菜单状态
                </span>
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.sys_normal_disable"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </template>
  </pageContainer>
</template>

<script>
import * as API from '@/api/menu/index'
import pageContainer from '../../../components/pageContainer.vue'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { dict } from '@/utils/dict'
export default {
  components: { Treeselect, pageContainer },
  data() {
    return {
      title: '',
      open: false,
      queryParams: {},
      dict: dict,
      statusOptions: [
        {
          label: '正常',
          value: '0'
        },
        {
          label: '停用',
          value: '1'
        }
      ],
      // 重新渲染表格状态
      refreshTable: true,
      // 遮罩层
      loading: true,
      // 菜单表格树数据
      menuList: [],
      // 是否展开，默认全部折叠
      isExpandAll: false,
      form: {},
      // 表单校验
      rules: {
        menuName: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '菜单顺序不能为空', trigger: 'blur' }
        ],
        path: [
          { required: true, message: '路由地址不能为空', trigger: 'blur' }
        ]
      },
      // 菜单树选项
      menuOptions: []
    }
  },
  mounted() {
    this.getTableList()
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        menuId: undefined,
        parentId: 0,
        menuName: undefined,
        icon: undefined,
        menuType: 'M',
        orderNum: undefined,
        isFrame: '1',
        isCache: '0',
        visible: '0',
        status: '0'
      }
      this.resetForm('form')
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      API.listMenu().then(response => {
        this.menuOptions = []
        const menu = { menuId: 0, menuName: '主类目', children: [] }
        menu.children = this.handleTree(response.data, 'menuId')
        this.menuOptions.push(menu)
        console.log(this.menuOptions, 'this.menuOptions')
      })
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      }
    },
    // 表格查询
    getTableList() {
      this.loading = true
      const params = {
        ...this.queryParams
      }
      API.listMenu(params).then(res => {
        if (res.code === 200) {
          this.menuList = this.handleTree(res.data, 'menuId')
          this.loading = false
        }
      })
    },
    // 菜单名称搜索
    handleQuery() {
      this.getTableList()
    },
    // 重置查询条件
    resetQuery() {
      this.resetForm('queryForm')
      this.getTableList()
    },
    // 表单重置
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields()
      }
    },
    // 表格展开或折叠
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    // 编辑
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      API.getMenu(row.menuId).then(response => {
        if (response.code === 200) {
          this.form = response.data
          this.open = true
          this.title = '修改菜单'
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.menuName + '"的数据项？').then(function() {
        return API.delMenu(row.menuId)
      }).then(() => {
        this.getTableList()
        this.$message.success('删除成功')
      }).catch(() => {})
    },
    // 新增
    handleAdd(row) {
      this.reset()
      this.getTreeselect()
      if (row != null && row.menuId) {
        this.form.parentId = row.menuId
      } else {
        this.form.parentId = 0
      }
      this.open = true
      this.title = '添加菜单'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.menuId !== undefined) {
            API.updateMenu(this.form).then(response => {
              if (response.code === 200) {
                this.$message.success('修改成功')
                this.open = false
                this.getTableList()
              }
            })
          } else {
            API.addMenu(this.form).then(response => {
              if (response.code === 200) {
                this.$message.success('新增成功')
                this.open = false
                this.getTableList()
              }
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    }
  }
}
</script>

<style lang="scss" scoped>
.menu-container {
  .title {
    display: flex;
    align-items: center;
    height: 30px;
    .line {
      width: 5px;
      height: 25px;
      background: #409eff;
      margin-right: 5px;
    }
    .text {
      color: #fff;
    }
  }
  .search {
    height: calc(100% - 30px);
    display: flex;
    align-items: center;
    .demo-form-inline {
      padding-left: 10px;
      :deep(.el-form-item) {
        margin-bottom: 0;
      }
    }
  }
  .table {
    height: calc(100% - 40px);
    margin-top: 10px;
    :deep(.el-table__body-wrapper){
      max-height: 544px;
      overflow-y: auto;
    }
    :deep(.el-icon-arrow-right){
      color: #c9e9ffc9;
    }
  }
}
/*最外层透明*/
:deep(.el-table),
:deep(.el-table__expanded-cell) {
  background-color: transparent;
}
/* 表格内背景颜色 */
:deep(.el-table th),
:deep(.el-table tr),
:deep(.el-table td) {
  background-color: transparent;
  color: #c9e9ff;
  height: 30px;
}
:deep(.el-table thead) {
  color: #7ec1ff;
  font-size: 14px;
}
:deep(.el-table--enable-row-hover) {
  background-color: transparent;
}
:deep(.el-table .warning-row) {
  background: rgba(51, 91, 127, 0.6);
}
:deep(.el-table) {
  --el-table-border-color: rgba(51, 91, 127, 0.6) !important;
}
:deep(.el-table tbody tr:hover > td) {
  background: rgba(51, 91, 127, 0.4) !important;
}
:deep(.el-input__inner) {
  background: rgba(51, 91, 127, 1);
  box-shadow: none;
  border: none;
  color: #fff;
}
:deep(.el-pager li) {
  background: rgba(51, 91, 127, 1);
  box-shadow: none;
  border: none;
  color: #fff;
}
:deep(.el-pager li.active) {
  background: rgb(25, 133, 227);
}
:deep(.btn-prev),
:deep(.el-pagination .btn-next) {
  background: rgba(51, 91, 127, 1);
  box-shadow: none;
  border: none;
  color: #fff;
}
:deep(.el-pagination button:disabled) {
  background: rgba(51, 91, 127, 1);
  box-shadow: none;
  border: none;
  color: #fff;
}
:deep(.el-pagination__total),
:deep(.el-pagination__jump) {
  color: #fff;
}
:deep(.el-table td.el-table__cell),
:deep(.el-table th.el-table__cell.is-leaf) {
  border-color: rgba(231, 234, 241, 0.2);
}
:deep(.el-table::before) {
  background: rgba(231, 234, 241, 0.2);
}
:deep(.el-form-item__label) {
  color: #a3b3be;
}
:deep(.el-dialog) {
  background: rgba(0, 28, 55, 0.85);
}
:deep(.el-dialog__header) {
  border-bottom: 1px solid rgba(120, 189, 255, 1);
  margin-right: 0px;
  margin-bottom: 10px;
}
:deep(.el-dialog__title) {
  color: #41b0ff;
  font-size: 16px;
}

:deep(.el-dialog__headerbtn .el-dialog__close) {
  color: rgba(70, 174, 255, 1);
}
</style>
